<?php

namespace app\api\controller;


use app\api\model\CartModel;
use app\api\model\OrderModel;
use app\api\utils\jsonResult;
use think\Controller;
use think\Log;
use think\Session;

class Order extends Controller
{
    public function addOrder(): \think\response\Json
    {
        if (Session::get('user_id')) {
            $user_id = Session::get('user_id');
            $orderModel = new OrderModel();
            //          $user_id = input('param.user_id');
            $cartId = input('param.cartId');
            $cartId = explode(",", $cartId);
            $cart = new CartModel();

            for ($i = 0; $i < count($cartId); $i++) {
                $cartList[$i] = $cart->getById($cartId[$i]);
            }

            $order_no = time() + $user_id;
            $order_date = date('Y-m-d H:i:s', time());
            for ($i = 0; $i < count($cartList); $i++) {
                $img = $cart->getImgByTitle($cartList[$i][0]['goods_title']);
                $data = [
                    'order_no' => $order_no,
                    'user_id' => $user_id,
                    'total' => $cartList[$i][0]['goods_totalPrice'],
                    'total_pay' => $cartList[$i][0]['goods_totalPrice'],
                    'pro_num' => $cartList[$i][0]['goods_num'],
                    'pro_title' => $cartList[$i][0]['goods_title'],
                    'sku_id' => $cartList[$i][0]['sku_id'],
                    'sku_title' => $cartList[$i][0]['sku_title'],
                    'pro_price' => $cartList[$i][0]['goods_price'],
                    'order_date' => $order_date,
                    'img' => $img
                ];
                $cartData = $orderModel->add($data);
            }
            if ($cartData) {
                for ($i = 0; $i < count($cartId); $i++) {
                    $cart->delete($cartId[$i]);
                }
                return (new jsonResult)::result(200, "订单添加成功", $cartList);
            } else return (new jsonResult)::result(500, "订单添加失败");
        } else return (new jsonResult)::result(401, "请先登录");
    }

    public function getOrder(): \think\response\Json
    {
        if (Session::get('user_id')) {
            $user_id = Session::get('user_id');
            $orderModel = new OrderModel();
            //            $user_id = input('param.user_id');
            $orderNo = $orderModel->getOrderNo($user_id);
            if ($orderNo && count($orderNo)) {
                for ($i = 0; $i < count($orderNo); $i++) {
                    $orders = $orderModel->getByNo($orderNo[$i]['order_no']);
                    if (!count($orders)) continue;
                    $productInfo = $orders[0];
                    $tmp['order_no'] = $productInfo['order_no'];
                    $tmp['order_date'] = $productInfo['order_date'];
                    for ($j = 0; $j < count($orders); $j++) {
                        $info['pro_title'] = $orders[$j]['pro_title'];
                        $info['total'] = $orders[$j]['total'];
                        $info['total_pay'] = $orders[$j]['total_pay'];
                        $info['pro_price'] = $orders[$j]['pro_price'];
                        $info['sku_id'] = $orders[$j]['sku_id'];
                        $info['sku_title'] = $orders[$j]['sku_title'];
                        $info['pro_num'] = $orders[$j]['pro_num'];
                        $info['id'] = $orders[$j]['id'];
                        $info['img'] = $orders[$j]['img'];
                        $info['order_no'] = $orders[$j]['order_no'];
                        $products[$j] = $info;
                    }
                    $tmp['products'] = $products;
                    //                    $orderNo[$i] = $orderModel->getByNo($orderNo[$i]['order_no']);
                    $orderNo[$i] = $tmp;
                    $products = null;
                }
                return (new jsonResult)::result(200, "获取订单信息成功", $orderNo);
            } else return (new jsonResult)::result(200, "无订单信息");
        } else return (new jsonResult)::result(401, "请先登录");
    }
}
